package com.eternal.framework.component;

import android.app.Application;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleObserver;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;

import com.trello.rxlifecycle2.LifecycleProvider;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * @author li
 */
public class BaseViewModel extends AndroidViewModel implements Consumer<Disposable>, LifecycleObserver {
    private IActive active;
    /**
     * 弱引用持有
     */
    private WeakReference<LifecycleProvider> lifecycle;
    /**
     * 管理RxJava，主要针对RxJava异步操作造成的内存泄漏
     */
    private CompositeDisposable mCompositeDisposable;

    public BaseViewModel(@NonNull Application application) {
        super(application);
        mCompositeDisposable = new CompositeDisposable();
    }

    protected void addSubscribe(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    public void registerRxBus(){}


    public void unregisterRxBus(){}

    /**
     * 注入RxLifecycle生命周期
     *
     * @param lifecycle
     */
    public void injectLifecycleProvider(LifecycleProvider lifecycle) {
        this.lifecycle = new WeakReference<>(lifecycle);
    }

    public LifecycleProvider getLifecycleProvider() {
        return lifecycle.get();
    }

    public void initActive(IActive active){
        this.active=active;
    }

    public void showDialog(Disposable disposable) {
        showDialog("请稍后...", disposable);
    }

    public void showDialog(String title, Disposable disposable) {
        active.showDialog(title, disposable);
    }

    public void dismissDialog() {
        active.dismissDialog();
    }

    public void dialogDisposable(Disposable disposable){
        active.dialogDisposable(disposable);
    }

    /**
     * 跳转页面
     *
     * @param clz 所跳转的目的Activity类
     */
    public void startActivity(Class<?> clz) {
        getApplication().startActivity(active.createIntent(clz));
    }

    /**
     * 跳转页面
     *
     * @param clz    所跳转的目的Activity类
     * @param bundle 跳转所携带的信息
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent=active.createIntent(clz);
        intent.putExtras(bundle);
        getApplication().startActivity(intent);
    }

    /**
     * 关闭界面
     */
    public void finish() {
        active.finish();
    }

    /**
     * 返回上一层
     */
    public void onBackPressed() {
        active.onBackPressed();
    }

    @Override
    protected void onCleared() {
        //ViewModel销毁时会执行，同时取消所有异步任务
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
    }

    @Override
    public void accept(Disposable disposable) {
        addSubscribe(disposable);
    }
}
